package com.tools.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tools.common.beans.vo.Result;
import com.tools.common.utils.vo.ResultTable;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * lenovo
 * @author ShangJY
 */
public class BaseController
{
    /**
     * 设置请求分页数据
     * @param page 起始页
     * @param size (＾－＾)页数据大小
     * @param orderBy 排序字段
     */
    protected void startPage(Integer page,Integer size,String orderBy)
    {
        PageHelper.startPage(page, size, orderBy);
    }

    protected void startPage(Integer page,Integer size)
    {
        PageHelper.startPage(page, size);
    }

    /**
     * Describe: 返回数据表格数据 分页
     * Param data
     * Return 表格分页数据
     */
    protected static ResultTable pageTable(Object data, long count) {
        return ResultTable.pageTable(count, data);
    }

    /**
     * 页面跳转
     */
    public ModelAndView jumpPage(String path) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(path);
        return modelAndView;
    }

    /**
     * 带参数的页面跳转
     */
    public ModelAndView jumpPage(String path, Map<String, ?> params) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(path);
        modelAndView.addAllObjects(params);
        return modelAndView;
    }

    protected Result getDataTable(List<?> list)
    {
        Result r = Result.OK(list);
        r.setCount((int)new PageInfo(list).getTotal());
        return r;
    }
}
